﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.cms.businesslogic.web;
using OpenRasta.Web;
using uREST.Blog.Handlers;
using uREST.CMS.Extensions;

namespace uREST.Blog.Extensions
{
    public static class DocumentExtensions
    {
        #region Blog

        public static Resources.Blog ToBlogResource(this Document doc)
        {
            return new Resources.Blog
            {
                Id = doc.Id,
                Href = typeof(Resources.Blog).CreateUri("Blog", new { id = doc.Id }).ToString(),
                Name = doc.getProperty("blogName") != null ? doc.getProperty("blogName").Value.ToString() : doc.Text,
                Description = doc.getProperty("blogDescription") != null ? doc.getProperty("blogDescription").Value.ToString() : "",
                PostsHref = typeof(List<Resources.Post>).CreateUri("PostList", new { blogId = doc.Id }).ToString(),
                CommentsHref = typeof(List<Resources.Comment>).CreateUri("CommentList", new { blogId = doc.Id }).ToString()
            };
        }

        public static List<Resources.Blog> ToBlogResourceList(this Document[] docs)
        {
            return new List<Document>(docs).ToBlogResourceList();
        }

        public static List<Resources.Blog> ToBlogResourceList(this List<Document> docs)
        {
            var resourceList = new List<Resources.Blog>();
            resourceList.AddRange(docs.Select(doc => doc.ToBlogResource()));
            return resourceList;
        }

        #endregion

        #region Post

        public static Resources.Post ToPostResource(this Document doc)
        {
            var blogDoc = doc.FindAncestorByDocTypeAlias(BlogHandler.DOC_TYPE_ALIAS);

            return new Resources.Post
            {
                Id = doc.Id,
                Href = typeof(Resources.Post).CreateUri("Post", new { id = doc.Id }).ToString(),
                BlogId = blogDoc != null ? blogDoc.Id : 0,
                BlogHref = blogDoc != null ? typeof(Resources.Blog).CreateUri("Blog", new { id = blogDoc.Id }).ToString() : "",
                Title = doc.Text,
                Content = doc.getProperty("bodyText") != null ? doc.getProperty("bodyText").Value.ToString() : "",
                Tags = doc.getProperty("tags") != null ? doc.getProperty("tags").Value.ToString() : "",
                CloseComments = doc.getProperty("closeComments") != null ? doc.getProperty("bodyText").Value.ToString() == "1" : false,
                CommentsHref = typeof(List<Resources.Comment>).CreateUri("CommentList", new { postId = doc.Id }).ToString(),
                IsPublished = doc.Published,
                PostDate = doc.getProperty("postDate") != null ? DateTime.Parse(doc.getProperty("postDate").Value.ToString()) : DateTime.MinValue,
                ReleaseDate = doc.ReleaseDate,
                ExpireDate = doc.ExpireDate
            };
        }

        public static List<Resources.Post> ToPostResourceList(this Document[] docs)
        {
            return new List<Document>(docs).ToPostResourceList();
        }

        public static List<Resources.Post> ToPostResourceList(this List<Document> docs)
        {
            var resourceList = new List<Resources.Post>();
            resourceList.AddRange(docs.Select(doc => doc.ToPostResource()));
            return resourceList;
        }

        #endregion
    }
}
